package com.binsoft.ndtta;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import com.binsoft.ndtta.R;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class FixtureAdapter extends BaseAdapter 
{
	private static final String TAG = "FixtureAdapter";
	private ArrayList<Match> mMatches = null;
	private Context mContext = null;
	private int mDivision = -1;
	private int mWeek = 0;
	private NdttaDatabaseAdapter mDatabaseAdapter = null;
	
	FixtureAdapter( NdttaDatabaseAdapter databaseAdapter, Context context, int week, int division, int team )
	{
		mWeek = week;
		mContext = context;
		mDivision = division;
		mDatabaseAdapter = databaseAdapter;
		
		// Get matches
		mMatches = Matches.getTeamMatches( team );
		
		// Set the division on the matches, this also sets the match date
		for ( Match match : mMatches )
		{
			match.division( mDivision );
		}
	}
	
	@Override
	public int getCount() 
	{
		return mMatches.size();
	}

	@Override
	public Object getItem(int position) 
	{
		return mMatches.get( position );
	}

	@Override
	public long getItemId(int position) 
	{
		return 0;
	}

	private void updateField( RelativeLayout layout, int field, String value )
	{
		TextView tv = (TextView) layout.findViewById(field);
		tv.setText( value );
	}

	private void showField( RelativeLayout layout, int field, boolean show )
	{
		TextView tv = (TextView) layout.findViewById(field);
		
		if ( show )
		{
			tv.setVisibility( View.VISIBLE );
		}
		else
		{
			tv.setVisibility( View.GONE );
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		RelativeLayout rowLayout;
		
		Match match = (Match)getItem( position );

		Team homeTeam = Matches.getTeam( mDivision, match.home() );
		Team awayTeam = Matches.getTeam( mDivision, match.away() );

		if (convertView == null) 
		{
			rowLayout = (RelativeLayout) LayoutInflater.from(mContext).inflate(
					R.layout.main, parent, false);
		} 
		else 
		{
			rowLayout = (RelativeLayout) convertView;
		}
		
		if ( mWeek == position )
		{
			rowLayout.setBackgroundResource( R.drawable.currentweekshape );
		}
		else if ( homeTeam == null && awayTeam == null )
		{
			// Free week so use the freeweek background
			rowLayout.setBackgroundResource( R.drawable.freeweekshape );
		}
		else if ( homeTeam.name().isEmpty() || awayTeam.name().isEmpty() )
		{
			rowLayout.setBackgroundResource( R.drawable.byeweekshape );
		}
		else
		{
			rowLayout.setBackgroundResource( R.drawable.myshape );
		}

		Log.i(TAG, "Week:" + mWeek + " Home:" + match.home() + " Away:" + match.away() );
		
		if ( homeTeam == null )
		{
			updateField( rowLayout, R.id.homeTeamField, match.description() );
		}
		else if ( homeTeam.name().isEmpty() )
		{
			updateField( rowLayout, R.id.homeTeamField, "BYE" );
		}
		else
		{
			updateField( rowLayout, R.id.homeTeamField, homeTeam.name() );
		}
		
		boolean showField = false;
		
		if ( awayTeam == null )
		{
		}
		else if  ( awayTeam.name().isEmpty() )
		{
			updateField( rowLayout, R.id.awayTeamField, "BYE" );
			showField = true;
		}
		else
		{
			updateField( rowLayout, R.id.awayTeamField, awayTeam.name() );
			showField = true;
		}

		showField( rowLayout, R.id.awayTeamField, showField );
		showField( rowLayout, R.id.versusField, showField );

		SimpleDateFormat format = new SimpleDateFormat(Constants.DATE_FORMAT);
		updateField( rowLayout, R.id.dateField, format.format( match.date() ) );
	
		// See if the game already exists
		Cursor prevMatch = mDatabaseAdapter.fetchMatch( match,false,true );
		
		TextView preHomeScore = (TextView)rowLayout.findViewById(R.id.prevHomeScore );
		TextView preAwayScore = (TextView)rowLayout.findViewById(R.id.prevAwayScore );

		if ( null != prevMatch && prevMatch.getCount() > 0 )
		{
			
			int homeScoreCol = prevMatch.getColumnIndex("homescore");
			int awayScoreCol = prevMatch.getColumnIndex("awayscore");
			
			int homeScore = prevMatch.getInt( homeScoreCol );
			int awayScore = prevMatch.getInt( awayScoreCol );

			preHomeScore.setText("(" + homeScore + ")");
			preAwayScore.setText("(" + awayScore + ")");

			preHomeScore.setVisibility( View.VISIBLE );
			preAwayScore.setVisibility( View.VISIBLE );
		}
		else
		{
			preHomeScore.setVisibility( View.GONE );
			preAwayScore.setVisibility( View.GONE );
		}

		// Hide the map button, has no effect here.
		ImageView mapButton = (ImageView)rowLayout.findViewById(R.id.mapImageView );
		mapButton.setVisibility( View.GONE );
		
		return rowLayout;
	}
}


